package com.juyu.miliao.api.controller;

import java.util.List;

import javax.annotation.Resource;

import com.juyu.miliao.api.domain.vo.UserCashVo;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import net.sf.json.JSONArray;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import com.juyu.miliao.api.domain.UserCash;
import com.juyu.miliao.api.service.UserCashService;
import com.juyu.miliao.common.domain.Subject;

import net.sf.json.JSONObject;


/**
 * 用户提现
 *
 * @author wbf
 */
@RestController
@RequestMapping("/userCash")
public class UserCashController {
	@Resource
	UserCashService userCashService;

	/**
	 * 获取用户钻石数量-可提现金额
	 */
	@GetMapping("/wallet")
	public JSONObject getUserWallet(Authentication token) {
		Subject subject = (Subject) token.getPrincipal();
		return userCashService.getUserWallet(subject.getId());
	}

	/**
	 * 获取可提现金额列表
	 */
	@GetMapping("/number")
	public JSONArray getCashNumberMap(){
		return userCashService.getCashNumberMap();
	}

	/**
	 * 绑定提现基础信息(支付宝账号、银行卡号)
	 * @param type 1 绑定支付宝，2绑定银行卡
	 */
	@PostMapping("/info/{type}")
	public void insertUserCashInfo(Authentication token,@PathVariable("type") int type , UserCash entity){
		Subject subject = (Subject) token.getPrincipal();
		if(type != 1){
			throw new ConsumeRuntimeException("类型错误");
		}
		if(entity.getZfbNumber().equals("") || entity.getZfbUserName().equals("") ||
				entity.getZfbNumber().length() < 2 || entity.getZfbNumber().length() > 40){
			throw new ConsumeRuntimeException("参数错误");
		}
		entity.setUserId(subject.getId());
		userCashService.insertUserCashInfo(entity);
	}


	/**
	 * 获取用户提现记录，创建时间倒序
	 */
	@GetMapping("/detail/{page}-{pageSize}")
	public List<UserCashVo> getUserCashList(Authentication token,
											@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
		Subject subject = (Subject) token.getPrincipal();
		if (page <= 0) {
			page = 1;
		}
		if (pageSize <= 0) {
			pageSize = 20;
		}
		return userCashService.getUserCashList(subject.getId(), page, pageSize);
	}

	/**
	 * 用户提现请求
	 * @param entity
	 */
	@PostMapping()
	public void submitUserCash(Authentication token,UserCash entity) {
		Subject subject = (Subject) token.getPrincipal();
		entity.setUserId(subject.getId());
		userCashService.submitUserCash(entity);
	}
}
